part of '../widgets.dart';

/// [VanPopup]属性常量
const kVanPopupBackground = VanCssKey._raw('--van-popup-background');
const kVanPopupTransition = VanCssKey._raw('--van-popup-transition');
const kVanPopupRoundRadius = VanCssKey._raw('--van-popup-round-radius');
const kVanPopupCloseIconSize = VanCssKey._raw('--van-popup-close-icon-size');
const kVanPopupCloseIconColor = VanCssKey._raw('--van-popup-close-icon-color');
const kVanPopupCloseIconMargin = VanCssKey._raw('--van-popup-close-icon-margin');
const kVanPopupCloseIconZIndex = VanCssKey._raw('--van-popup-close-icon-z-index');


/// [VanPopup]主题类
@immutable
class VanPopupTheme extends VanProvider<VanPopupThemeData> with VanProvide {
  const VanPopupTheme({super.key, required super.child, required super.provide});

  static VanPopupThemeData of(BuildContext context) => VanProvider.of<VanPopupTheme>(context)?.provide ?? VanTheme.of(context).popupTheme;
  static const _prefix = "--van-popup-";
  static bool support(VanCssKey key) => key.key.startsWith(_prefix);

  static dynamic get<T>(BuildContext context, {required VanCssKey key, T? defaultValue}) {
    final VanPopupThemeData popupTheme = VanPopupTheme.of(context);
    if (kVanPopupBackground == key) return popupTheme.background.get(context);
    if (kVanPopupTransition == key) return popupTheme.transition.get(context);
    if (kVanPopupRoundRadius == key) return popupTheme.roundRadius.get(context);
    if (kVanPopupCloseIconSize == key) return popupTheme.closeIconSize.get(context);
    if (kVanPopupCloseIconColor == key) return popupTheme.closeIconColor.get(context);
    if (kVanPopupCloseIconMargin == key) return popupTheme.closeIconMargin.get(context);
    if (kVanPopupCloseIconZIndex == key) return popupTheme.closeIconZIndex.get(context);
    return null;
  }
}

/// [VanPopup]主题数据类
@immutable
class VanPopupThemeData with VanProvide {
  final VanCss<VanBgColor> background;
  final VanCss<String> transition;
  final VanCss<BorderRadius> roundRadius;
  final VanCss<double> closeIconSize;
  final VanCss<Color> closeIconColor;
  final VanCss<EdgeInsets> closeIconMargin;
  final VanCss<String> closeIconZIndex;

  /// 构造器
  const VanPopupThemeData.raw({
    required this.background,
    required this.transition,
    required this.roundRadius,
    required this.closeIconSize,
    required this.closeIconColor,
    required this.closeIconMargin,
    required this.closeIconZIndex,
  });

  /// 拷贝
  VanPopupThemeData copyFrom({
    VanCss<VanBgColor>? background,
    VanCss<String>? transition,
    VanCss<BorderRadius>? roundRadius,
    VanCss<double>? closeIconSize,
    VanCss<Color>? closeIconColor,
    VanCss<EdgeInsets>? closeIconMargin,
    VanCss<String>? closeIconZIndex,
  }) {
    return VanPopupThemeData.raw(
      background: background ?? this.background,
      transition: transition ?? this.transition,
      roundRadius: roundRadius ?? this.roundRadius,
      closeIconSize: closeIconSize ?? this.closeIconSize,
      closeIconColor: closeIconColor ?? this.closeIconColor,
      closeIconMargin: closeIconMargin ?? this.closeIconMargin,
      closeIconZIndex: closeIconZIndex ?? this.closeIconZIndex,
    );
  }

  /// 反序列化
  static VanPopupThemeData fromJson(Map<String, dynamic> json) {
    return VanPopupThemeData.raw(
      background: VanCss(json[kVanPopupBackground.key]),
      transition: VanCss(json[kVanPopupTransition.key]),
      roundRadius: VanCss(json[kVanPopupRoundRadius.key]),
      closeIconSize: VanCss(json[kVanPopupCloseIconSize.key]),
      closeIconColor: VanCss(json[kVanPopupCloseIconColor.key]),
      closeIconMargin: VanCss(json[kVanPopupCloseIconMargin.key]),
      closeIconZIndex: VanCss(json[kVanPopupCloseIconZIndex.key]),
    );
  }

}

/// [VanPopup]样式
@immutable
class VanPopupStyle with VanProvide {
  static of(BuildContext context) => VanProvider.of<VanPopupStyle>(context);

  final VanBgColor? background;
  final String? transition;
  final BorderRadius? radius;
  final double? closeIconSize;
  final Color? closeIconColor;
  final EdgeInsets? closeIconMargin;
  final String? closeIconZIndex;

  VanPopupStyle({
    this.background,
    this.transition,
    this.radius,
    this.closeIconSize,
    this.closeIconColor,
    this.closeIconMargin,
    this.closeIconZIndex,
  });

  bool get isNonNull {
    if (background == null) return false;
    if (transition == null) return false;
    if (radius == null) return false;
    if (closeIconSize == null) return false;
    if (closeIconColor == null) return false;
    if (closeIconMargin == null) return false;
    if (closeIconZIndex == null) return false;
    return true;
  }

  VanPopupStyle copyFrom({
    VanBgColor? background,
    String? transition,
    BorderRadius? radius,
    double? closeIconSize,
    Color? closeIconColor,
    EdgeInsets? closeIconMargin,
    String? closeIconZIndex,
  }) {
    return VanPopupStyle(
      background: background ?? this.background,
      transition: transition ?? this.transition,
      radius: radius ?? this.radius,
      closeIconSize: closeIconSize ?? this.closeIconSize,
      closeIconColor: closeIconColor ?? this.closeIconColor,
      closeIconMargin: closeIconMargin ?? this.closeIconMargin,
      closeIconZIndex: closeIconZIndex ?? this.closeIconZIndex,
    );
  }
}